package test;

import gradation.GradationFactory;
import histogram.GrayscaleHistogram;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import strengthen.spatial.FilterFactory;

public class PictureFrame extends JFrame {

	private JPanel contentPane;
	private BufferedImage image;
	private ImagePanel imagePanel;
	private HistogramPanel histogramPanel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PictureFrame frame = new PictureFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public PictureFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 791, 608);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		imagePanel = new ImagePanel();

		JButton inverseGrayBtn = new JButton("\u7070\u5EA6\u53CD\u8F6C");
		inverseGrayBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				inverseGray();
			}
		});

		histogramPanel = new HistogramPanel();

		JButton histogramBtn = new JButton("\u751F\u6210\u76F4\u65B9\u56FE");
		histogramBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				GrayscaleHistogram.getInstance().grayscaleCount(image);
				histogramPanel.setData(GrayscaleHistogram.getInstance()
						.getGrayscaleStatistics(), GrayscaleHistogram
						.getInstance().getTotalPixels());
			}
		});

		JButton openPictureBtn = new JButton("\u6253\u5F00\u56FE\u50CF");
		openPictureBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				JFileChooser chooser = new JFileChooser();
				chooser.setFileFilter(new FileNameExtensionFilter("JPG | JPEG",
						"jpg", "jpeg"));
				int res = chooser.showOpenDialog(((JButton) evt.getSource())
						.getParent());

				if (res == JFileChooser.APPROVE_OPTION) {
					File f = chooser.getSelectedFile();
					showImage(f);
				}
			}
		});

		JButton thresholdBtn = new JButton("\u4E8C\u503C\u53D8\u6362");
		thresholdBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				thresholdTrans();
			}
		});

		JButton gradationBtn = new JButton("\u7070\u5EA6\u5316");
		gradationBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				gradation();
			}
		});

		JButton avgFilterBtn = new JButton("\u5747\u503C\u6EE4\u6CE2");
		avgFilterBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				avgFilter();
			}
		});

		JButton medianFilterBtn = new JButton("\u4E2D\u503C\u6EE4\u6CE2");
		medianFilterBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				medianFilter();
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane
				.setHorizontalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addComponent(
																				imagePanel,
																				GroupLayout.PREFERRED_SIZE,
																				456,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(18)
																		.addComponent(
																				histogramPanel,
																				GroupLayout.DEFAULT_SIZE,
																				281,
																				Short.MAX_VALUE))
														.addGroup(
																gl_contentPane
																		.createParallelGroup(
																				Alignment.LEADING,
																				false)
																		.addGroup(
																				gl_contentPane
																						.createSequentialGroup()
																						.addComponent(
																								openPictureBtn)
																						.addPreferredGap(
																								ComponentPlacement.UNRELATED)
																						.addComponent(
																								gradationBtn,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE))
																		.addGroup(
																				gl_contentPane
																						.createSequentialGroup()
																						.addComponent(
																								inverseGrayBtn)
																						.addPreferredGap(
																								ComponentPlacement.UNRELATED)
																						.addComponent(
																								histogramBtn))
																		.addGroup(
																				gl_contentPane
																						.createSequentialGroup()
																						.addComponent(
																								thresholdBtn)
																						.addPreferredGap(
																								ComponentPlacement.UNRELATED)
																						.addGroup(
																								gl_contentPane
																										.createParallelGroup(
																												Alignment.LEADING)
																										.addComponent(
																												medianFilterBtn,
																												Alignment.TRAILING,
																												GroupLayout.DEFAULT_SIZE,
																												93,
																												Short.MAX_VALUE)
																										.addComponent(
																												avgFilterBtn,
																												GroupLayout.DEFAULT_SIZE,
																												GroupLayout.DEFAULT_SIZE,
																												Short.MAX_VALUE)))))
										.addContainerGap()));
		gl_contentPane
				.setVerticalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.TRAILING,
																false)
														.addComponent(
																histogramPanel,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																imagePanel,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																366,
																Short.MAX_VALUE))
										.addGap(18)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																openPictureBtn)
														.addComponent(
																gradationBtn))
										.addGap(10)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																inverseGrayBtn)
														.addComponent(
																histogramBtn))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																thresholdBtn)
														.addComponent(
																avgFilterBtn))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addComponent(medianFilterBtn)
										.addContainerGap(54, Short.MAX_VALUE)));
		contentPane.setLayout(gl_contentPane);

		init();
	}

	private void init() {

	}

	private void showImage(File f) {
		try {
			image = ImageIO.read(f);
			imagePanel.setImage(image);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void gradation() {
		GradationFactory.getGradationRGBAverage().process(image.getData(),
				image.getRaster());
		imagePanel.repaint();
	}

	private void inverseGray() {
		GradationFactory.getGrayInverseTrans().process(image.getData(),
				image.getRaster());
		imagePanel.repaint();
	}

	private void thresholdTrans() {
		GradationFactory.getThresholdTrans().process(image.getData(),
				image.getRaster());
		imagePanel.repaint();
	}

	private void avgFilter() {
		FilterFactory.getAverageFilter(3).process(image.getData(),
				image.getRaster());
		imagePanel.repaint();
	}

	private void medianFilter() {
		FilterFactory.getMedianFilter(3).process(image.getData(),
				image.getRaster());
		imagePanel.repaint();
	}
}
